package dao.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.jdbc.DBUtils;

public abstract class AbstractDao<T> {
	//查询服务
	protected List<T> find(String sql,Object...param){
		List<T> list=null;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBUtils.getConnection();
			ps=DBUtils.prepareStatement(con, sql, param);
			rs=ps.executeQuery();
			list=new ArrayList<T>();
			while(rs.next()){
				list.add(rowMap(rs));//行映射
			}
		} catch (SQLException e) {
			throw new RuntimeException();
		}finally{
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return list;
	}
	
	//根据id来查询
	protected T findById(String sql,Object...param){
		T t=null;
		List<T> list=null;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBUtils.getConnection();
			ps=DBUtils.prepareStatement(con, sql, param);
			rs=ps.executeQuery();
			list=new ArrayList<T>();
			while(rs.next()){
				list.add(rowMap(rs));//行映射
			}
		} catch (SQLException e) {
			throw new RuntimeException();
		}finally{
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		if(list!=null && list.size()>0){
			t=list.get(0);
		}
		return t;
	}
	
	
	
	
	//取得第一条记录的第一个字段
	protected Object findFirstField(String sql,Object...param){
		Object data=null;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBUtils.getConnection();
			ps=DBUtils.prepareStatement(con, sql, param);
			rs=ps.executeQuery();
			if(rs.next()){
				data=rs.getObject(1);
			}
		} catch (SQLException e) {
			throw new RuntimeException();
		}finally{
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return data;
	}
	
	//执行服务
	public int execute(String sql,Object...param){
		int tag=0;
		Connection con=null;
		PreparedStatement ps=null;
		try {
			con=DBUtils.getConnection();
			ps=DBUtils.prepareStatement(con, sql, param);
			tag=ps.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException();
		}finally{
			try {
				ps.close();
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return tag;
	}
	
	//执行服务并返回主键值
	public Long executeReturnKey(String sql,Object...param){
		Long generatedKey=null;
		Connection con=null;
		PreparedStatement ps=null;
		try {
			con=DBUtils.getConnection();
			ps=DBUtils.prepareStatement(con, sql, param);
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				generatedKey=rs.getLong(1);
			}
		}catch (SQLException e) {
			throw new RuntimeException();
		}finally{
			try {
				ps.close();
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return generatedKey;
	}
	
	/**
	 * 行映射
	 * */
	public abstract T rowMap(ResultSet rs);
}
